package com.testingedu.action.background;

import com.testingedu.jdbcDao.TCategoryDao;
import com.testingedu.jdbcDao.TTrainInfoDao;
import com.testingedu.model.TCategory;
import com.testingedu.model.TTrainInfo;

public class TTrainInfoAction extends RequestContext {

	private static final long serialVersionUID = -1381793417198538680L;
	private TTrainInfo tTrainInfo;
	private static TTrainInfoDao tTrainInfoDao = new TTrainInfoDao();

	public TTrainInfo gettTrainInfo() {
		return tTrainInfo;
	}

	public void settTrainInfo(TTrainInfo tTrainInfo) {
		this.tTrainInfo = tTrainInfo;
	}

	public void _add() throws Exception {
		String url = request.getParameter("url");
		if ("jsp".equals(url)) {
			createQueryResult();
			new TCategoryDao().list(queryResult, null);
			if (queryResult.getResultList() != null
					|| queryResult.getResultList().size() > 0) {
				request.setAttribute("queryResult", queryResult);
				request.getRequestDispatcher(
						"/WEB-INF/admin/jsp/trainInfo/add.jsp").forward(
						request, response);
			} else {
				request.setAttribute("message", "请先添加类型，再添加信息！");
				request.getRequestDispatcher("/error/error.jsp").forward(
						request, response);
			}
		} else {
			StringBuffer msg = new StringBuffer();
			String errorMsg = null;
			boolean ok = true;
			Integer categoryId = Integer.parseInt(request
					.getParameter("categoryId"));
			TCategory tCategory = new TCategory(categoryId);
			tTrainInfo.settCategory(tCategory);
			tTrainInfo.settUser(getTUser());
			try {
				keywords();
			} catch (Exception e) {
				ok = false;
				errorMsg = "关键字只允许3个，请核对";
			}
			int id = 0;
			if (ok) {
				id = tTrainInfoDao.add(tTrainInfo);
			}
			msg.append("{\"id\":" + id + "");
			msg.append(",\"msg\":\"" + errorMsg + "\"}");
			response.getWriter().write(msg.toString());
			response.getWriter().close();
		}
	}

	public void _delete() throws Exception {
		tTrainInfoDao.delete(tTrainInfo.getId());
		response.sendRedirect("/testingedu2/admin/trainInfo/list");
	}

	public void _update() throws Exception {
		String url = request.getParameter("url");
		if ("update".equals(url)) {
			createQueryResult();
			new TCategoryDao().list(queryResult, null);
			request.setAttribute("queryResult", queryResult);
			this.info(url);
		} else {
			StringBuffer msg = new StringBuffer();
			String errorMsg = null;
			boolean ok = true;
			msg.append("{\"id\":" + tTrainInfo.getId() + "");
			Integer categoryId = Integer.parseInt(request
					.getParameter("categoryId"));
			TCategory tCategory = new TCategory(categoryId);
			tTrainInfo.settCategory(tCategory);
			tTrainInfo.settUser(getTUser());
			try {
				keywords();
			} catch (Exception e) {
				errorMsg = "关键字只允许3个，请核对";
				ok = false;
			}
			if (ok) {
				tTrainInfoDao.update(tTrainInfo);
			}
			msg.append(",\"msg\":\"" + errorMsg + "\"}");
			response.getWriter().write(msg.toString());
			response.getWriter().close();
		}
	}

	public void _list() throws Exception {
		String url = request.getParameter("url");
		if ("info".equals(url)) {
			this.info(url);
		} else {
			createQueryResult();
			String type = request.getParameter("type");
			if(type != null && type.trim().length() > 0) {
				TCategory t = new TCategory();
				t.setName(type);
				if(tTrainInfo == null) {
					tTrainInfo = new TTrainInfo();
				}
				tTrainInfo.settCategory(t);
			}
			tTrainInfoDao.list(queryResult, tTrainInfo);
			request.setAttribute("queryResult", queryResult);
			request.getRequestDispatcher(
					"/WEB-INF/admin/jsp/trainInfo/list.jsp").forward(request,
					response);
		}
	}

	private void info(String url) throws Exception {
		tTrainInfo = tTrainInfoDao.info(tTrainInfo.getId());
		request.setAttribute("tTrainInfo", tTrainInfo);
		request.getRequestDispatcher(
				"/WEB-INF/admin/jsp/trainInfo/" + url + ".jsp").forward(
				request, response);
	}
	
	private void keywords() throws Exception {
		String keywords = request.getParameter("keywords");
		if (keywords != null && keywords.trim().length() > 0) {
			String[] keywordArray = keywords.split(",");
			switch (keywordArray.length) {
			case 0:
				tTrainInfo.setKeyword1("");
				tTrainInfo.setKeyword2("");
				tTrainInfo.setKeyword3("");
				break;
			case 1:
				tTrainInfo.setKeyword1(keywordArray[0]);
				tTrainInfo.setKeyword2("");
				tTrainInfo.setKeyword3("");
				break;
			case 2:
				tTrainInfo.setKeyword1(keywordArray[0]);
				tTrainInfo.setKeyword2(keywordArray[1]);
				tTrainInfo.setKeyword3("");
				break;
			case 3:
				tTrainInfo.setKeyword1(keywordArray[0]);
				tTrainInfo.setKeyword2(keywordArray[1]);
				tTrainInfo.setKeyword3(keywordArray[2]);
				break;
			default:
				throw new Exception();
			}
		} else {
			tTrainInfo.setKeyword1("");
			tTrainInfo.setKeyword2("");
			tTrainInfo.setKeyword3("");
		}
	}
}
